package com.zhoukai.business.service.license;

import com.zhoukai.business.util.HttpServletWrite;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * author: zkai
 * date: 2022/4/6 10:02
 * description: 接口访问之前进行证书校验
 * version: 1.0
 */
@Slf4j
@Order
@WebFilter(urlPatterns = {"/*"}, filterName = "licenseValidateFilter")
public class LicenseValidateFilter implements Filter {

    @Autowired
    LicenseVerify licenseVerify;

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //校验证书是否有效
        boolean verify = licenseVerify.verify();
        if (!verify) {
            HttpServletWrite.writeJson(response, "{\"code\":\"" + "-203" + "\", \"message\":\"您的证书无效，请核查服务器是否取得授权或重新申请证书！\", \"success\":\"false\"}");
            return;
        }
        chain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
